package com.vichat.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vichat.common.cache.RedisHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

public class UserUtil {
    private static Logger logger = LoggerFactory.getLogger(UserUtil.class);

    public static void checkToken(String content,long uid) throws Exception {

        String tokenRedis = RedisHelper.getUserToken(uid);
        if (StringUtils.isNotBlank(tokenRedis)) {
            if (StringUtils.equals(tokenRedis,content)) {
                return;
            }
        }
        String token = AES.decrypt(content);
        Properties properties = StringToPropertiesConverter.convert(token.replaceAll(",", "\n"));
        String uidInToken = properties.getProperty("uid");
        if (!StringUtils.equals(uidInToken, String.valueOf(uid))) {
            logger.error("token is illegal. uid="+uid+"; token="+content);
            throw new Exception("token is illegal");
        }

        RedisHelper.setUserToken(uid,content);
    }

    public static String dayComparePrecise(Date fromDate,Date toDate){
        Calendar from  =  Calendar.getInstance();
        from.setTime(fromDate);
        Calendar  to  =  Calendar.getInstance();
        to.setTime(toDate);
        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH);
        int fromDay = from.get(Calendar.DAY_OF_MONTH);
        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH);
        int toDay = to.get(Calendar.DAY_OF_MONTH);
        int year = toYear  -  fromYear;
        int month = Math.abs(toMonth  - fromMonth);
        int day = Math.abs(toDay  - fromDay);
        return year + "-" + month + "-" + day;
    }

    public static String getUserNamesByJson(String uids) {
        String userNameStr = "";
        JSONArray jsonArray = JSONArray.parseArray(uids);
        if (jsonArray != null && jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (StringUtils.isBlank(userNameStr)) {
                    userNameStr += jsonObject.getString("userName");
                } else {
                    userNameStr += "," + jsonObject.getString("userName");
                }
            }
        }
        return userNameStr;
    }

    public static String getUidsToJson(String uidsStr) {
        String[] uids = uidsStr.split(",");
        if (uids.length != 0) {
            JSONArray jsonArray = new JSONArray();
            for (String uid : uids) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("uid", uid);
                jsonObject.put("userName", RedisHelper.getUname(Long.parseLong(uid)));
                jsonArray.add(jsonObject);
            }
            return jsonArray.toJSONString();
        }
        return null;
    }

}
